"use strict";

(function() {
	var BOUNDS_PADDING = 2;
	
	alx.lj.Link = function (args) {
		var me = this;
		
		var bounds   = null,
				document = null,
				elem     = args.elem,
				canvas   = args.canvas,
				rect     = null,
				hit      = false,
				prefs    = alx.lj.prefs,
				rectCurrentAttributes = null;
		
				
		var init = function() {
			document = elem.ownerDocument;
			me.refreshBounds();
		};
		
		me.getElement = function () {
			return elem;
		};
		
		me.getHash = function () {
			return bounds.top+","+bounds.right+","+bounds.bottom+","+bounds.left;
		};
		
		me.showSelectionRect = function (canv) {
			var rca;
			
			canvas = canv;
			rca = rectCurrentAttributes;
			
			if (!rect || (rca.top != bounds.top) || (rca.left != bounds.left) || (rca.right != bounds.right) || (rca.bottom != bounds.bottom)) {
				initRect();
			}
			
			rect.show();
		};
		
		me.hideSelectionRect = function (canvas) {
			rect && rect.hide();
		};
		
		me.clearHit = function () { 
			hit = false; 
		};
		
		me.setHit = function () { 
			hit = true; 
		};
		
		me.isHit = function () { 
			return hit; 
		};
		
		me.getHref = function () { 
			return elem.href; 
		};
		
		me.hitTest = function (x, y) {
			if ((x >= bounds.left && x <= bounds.left + bounds.width) && (y >= bounds.top && y <= bounds.top + bounds.height)) {
				return true;
			}
			return false;
		};
			
		me.getBounds = function () {
			return bounds;
		};
		
		me.refreshBounds = function () {
			var computedStyle, 
					boundingRect, 
					padding;
			
			padding = prefs.visual.itemSelRectPadding() || 0;
			computedStyle = document.defaultView.getComputedStyle(elem);
			
			if ((computedStyle.width == "auto" || computedStyle.height == "auto") && (elem.children[0] instanceof Image)) {
				boundingRect = elem.children[0].getBoundingClientRect();
			} else {
				boundingRect = elem.getBoundingClientRect();
			}
			
			if (boundingRect) {
				bounds = {
					top:    boundingRect.top    + document.defaultView.pageYOffset - padding,
					left:   boundingRect.left   + document.defaultView.pageXOffset - padding,
					bottom: boundingRect.bottom + document.defaultView.pageYOffset + padding,
					right:  boundingRect.right  + document.defaultView.pageXOffset + padding
				};
			}
			else {
				bounds = null;
			}
		};
		
		var calcBounds = function () {
			
		};
				
		var initRect = function () {
			var svgAttr = {
				"x":               bounds.left,
				"y":               bounds.top,
				"width":           bounds.right  - bounds.left,
				"height":          bounds.bottom - bounds.top,
				"fill":            prefs.visual.itemSelRectColor(),
				"fill-opacity":    prefs.visual.itemSelRectOpacity(),
				"stroke":          prefs.visual.itemSelRectColor(),
				"stroke-width":    prefs.visual.itemSelRectBorderWidth(),
				"shape-rendering": "crispEdges",
				"stroke-lineJoin": "round"
			};
			
			if (rect) {
				rect.setAttributes(svgAttr);
			} else {
				rect = new alx.svg.Rect(svgAttr);
			}
			
			rectCurrentAttributes = svgAttr;
			if (!rect.isAttached()) canvas.append(rect);
		};
	
		
		// ---------- //
		init();
	};
})();